

package com.walk.zworld.square.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.loadmore.LoadMoreStatus
import com.walk.zworld.R
import com.base.lib.base.adapter.CustomerFooterAdapter

/**
 * PagingDataAdapter添加Footer
 * @since  2021/5/21
 */
class FooterAdapter(val retry: () -> Unit) : CustomerFooterAdapter<FooterAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val progressBar: ProgressBar = itemView.findViewById(R.id.progressBar)
        val retryButton: TextView = itemView.findViewById(R.id.retryButton)
    }

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadMoreStatus): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_footer_item, parent, false)
        val holder = ViewHolder(view)
        holder.retryButton.setOnClickListener {
            retry()
        }
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, loadState: LoadMoreStatus) {
        holder.progressBar.isVisible = loadState == LoadMoreStatus.Loading
        holder.retryButton.isVisible = loadState == LoadMoreStatus.Fail
    }

}